package com.cuit.mimall.service.impl;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cuit.mimall.common.R;
import com.cuit.mimall.entity.Category;
import com.cuit.mimall.entity.Product;
import com.cuit.mimall.entity.ShoppingCart;
import com.cuit.mimall.entity.User;
import com.cuit.mimall.mapper.ShoppingCartMapper;
import com.cuit.mimall.service.IProductService;
import com.cuit.mimall.service.IShoppingCartService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.data.annotation.Reference;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author TeaWu
 * @since 2023-07-04
 */
@Service
public class ShoppingCartServiceImpl extends ServiceImpl<ShoppingCartMapper,ShoppingCart> implements IShoppingCartService{


    /**
     * 添加购物车
     * @param shoppingCart 购物车信息
     * @return R<?>
     */
    @Override
    public R<?> add(ShoppingCart shoppingCart){
        if(super.save(shoppingCart)){
            return R.ok("添加成功");
        }else{
            return R.fail("添加失败");
        }
    }

    /**
     * 去重
     */
    @Reference
    private  IProductService productService;

    @Override
    public int addCart(ShoppingCart shoppingCart, User user) {
        return 0;
    }

    @Override
    public List<ShoppingCart> getUserCartList(User user){
        List<ShoppingCart> cartList=new ArrayList<ShoppingCart>();
        //查询商品信息
        for(ShoppingCart shoppingCart:cartList){
            Product product=productService.getBaseMapper().selectById(shoppingCart.getProductId());
            Integer num=shoppingCart.getNum();
        }
        //去重
        Set<Integer> set=new HashSet<>();
        Map<Integer,ShoppingCart> map=new HashMap<>();
        for(ShoppingCart shoppingCart:cartList){
            if(set.add(shoppingCart.getProductId())){
                map.put(shoppingCart.getProductId(),shoppingCart);
            }else{
                ShoppingCart mapCart=map.get(shoppingCart.getProductId());
                mapCart.setNum(mapCart.getNum()+shoppingCart.getNum());
            }
        }
        //清空集合
        cartList.clear();
        //把集合中的商品放入list
        Set<Map.Entry<Integer,ShoppingCart>> entries=map.entrySet();
        for(Map.Entry<Integer,ShoppingCart> entry:entries){
            cartList.add(entry.getValue());
        }
        return cartList;
    }

    /**
     * 删除购物车
     * @param ids 购物车id数组
     * @return R<?>
     */
    @Override
    public R<?> delete(Integer[] ids) {
        if (super.removeById((Serializable) Arrays.asList(ids))) {
            return R.ok("删除成功");
        } else {
            return R.fail("删除失败");
        }
    }

    /**
     * 分页查询购物车列表
     *
     * @param pageNum  页码
     * @param pageSize 每页数量
     * @return R<?>
     */
    @Override
    public R<?> getAll(Integer pageNum, Integer pageSize) {
        Page<ShoppingCart> page = new Page<>(pageNum, pageSize);
        return R.ok(super.page(page, null));
    }
}
